package com.attms.service.impl;

import com.attms.dao.AbsentDao;
import com.attms.dao.EmployeeDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.domain.AbsentStrategy.AbsentStrategySet;
import com.attms.service.IDManagerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

import static java.time.temporal.ChronoUnit.DAYS;
import static org.apache.commons.lang3.compare.ComparableUtils.ge;

@Service
public class DManagerServiceImpl extends ServiceImpl<AbsentDao, Absent> implements IDManagerService {
    @Autowired
    private AbsentDao absentDao;
    @Autowired
    private EmployeeDao employeeDao;

    @Override
    /**
     * 处理请假，同意与否，假条id，回复信息
     */
    public boolean solveLeave(Integer isagree, Absent absenter) {
        System.out.println(isagree + " " + absenter.getA_ID() + " " + absenter.getA_RESPON());
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getA_ID, absenter.getA_ID());
        Absent absence = absentDao.selectOne(wrapper);
        System.out.println(absence);
        // 如果部门经理同意，则依据类型和状态选择转1总经理审批 2人事审核 3申请通过4申请失败
        if (isagree == 0) {
            absence.setA_RESPON("Disagree with the leave of absence");
            absence.setA_STATE(4);
        }
        // else if(absence.getA_TYPE().equals(4)){//病假。则先交人事
        // absence.setA_RESPON("病假，材料交由人事部审核");
        // absence.setA_STATE(2);
        // }
        else if (absence.getA_STTIME().until(absence.getA_ENDTIME()).getDays() > 3) {
            absence.setA_RESPON(
                    "The length of time exceeds three days, the department manager has agreed to hand over to the general manager to deal with");
            absence.setA_STATE(1);
        } else {
            absence.setA_RESPON("The department manager has agreed to review and approve");
            absence.setA_STATE(3);
            int days = absence.getA_STTIME().until(absence.getA_ENDTIME()).getDays();
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++" + days);
            // 如果是因公请假，则不扣薪，只记录
            Employee nowperson = employeeDao.selectById(absence.getE_ID());
            if (absence.getA_TYPE() > 0) {// 如果是带薪假期，则只修改请的假的剩余天数
                int vocationType = absence.getA_TYPE();
                AbsentStrategySet absentStrategySet = new AbsentStrategySet();
                absentStrategySet.apply(vocationType, nowperson, days, absence);
                employeeDao.updateById(nowperson);

            }
        }
        absentDao.updateById(absence);
        return true;
    }

    // 部门经理生成个人请假记录(按照时间，默认为全时间段)
    public List<Absent> getPersonalAbsentList(Absent absent) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getE_ID, absent.getE_ID())
                .ge(Absent::getA_STTIME, absent.getA_STTIME())
                .le(Absent::getA_ENDTIME, absent.getA_ENDTIME());
        return absentDao.selectList(wrapper);
    }

    // 部门经理生成本部分请假记录(按照时间，默认为全时间段)
    public List<Absent> getAllAbsentList(Integer department, Absent absent) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Absent::getA_STTIME, absent.getA_STTIME())
                .le(Absent::getA_ENDTIME, absent.getA_ENDTIME())
                .eq(Absent::getA_DEPARTMENT, department);
        return absentDao.selectList(wrapper);
    }

    @Override
    public List<Absent> getAbsentList(Integer department) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getA_DEPARTMENT, department);
        // .ge(Absent::getA_STATE, 3);
        // select(Absent::getE_ID,Absent::getA_REASON,Absent::getA_STTIME,Absent::getA_ENDTIME);
        return absentDao.selectList(wrapper);
    }

    @Override
    public List<Absent> getAbsentListNosolve(Integer department) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getA_DEPARTMENT, department)
                .eq(Absent::getA_STATE, 0);
        return absentDao.selectList(wrapper);
    }
}
